% The function returns mutationChildren—the mutated offspring—as a matrix 
% whose rows correspond to the children. The number of columns
% of the matrix is Number of variables.
function mutationChildren = mutationFcn(parents, options, nvars, FitnessFcn,...
                                        state, thisScore, thisPopulation)
% parents        — Row vector of parents chosen by the selection function
% options        — Options structure
% nvars          — Number of variables
% FitnessFcn     — Fitness function
% state          — Structure containing information about the current 
%                  generation. The State Structure describes the fields of
%                  state.
% thisScore      — Vector of scores of the current population
% thisPopulation — Matrix of individuals in the current population

mutationChildren = thisPopulation(parents, 1:end);
j = 1;
for i = parents
    child = thisPopulation(i, :);
    f1 = FitnessFcn(child);
    
    m = randi([2 nvars]);
    n = randi([2 nvars]);
    
    tmp      = child(m);
    child(m) = child(n);
    child(n) = tmp;
    f2 = FitnessFcn(child);
    if f2 <= f1
        mutationChildren(j, :) = child;
    end
    j = j + 1;
end
